package com.atguigu.async;

import com.atguigu.exception.SleepUtils;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;

public class FutureDemo08 {
    public static void main(String[] args) throws Exception {
        thenAcceptParallel();
        SleepUtils.sleep(10);
    }

    /**
     * 你好supplyAsync
     * supplyFuture睡了两秒
     * thenApply2睡了两秒
     * ForkJoinPool.commonPool-worker-9第二个线程thenApply0722
     * thenApply1睡了两秒
     * ForkJoinPool.commonPool-worker-9第一个线程thenApply0722
     * 0722apply1
     * 0722apply2
     */
    public static void thenAcceptParallel() throws Exception {
        //具备先后顺序
        CompletableFuture<String> supplyFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("你好supplyAsync");
            SleepUtils.sleep(2);
            System.out.println("supplyFuture睡了两秒");
            return "0722";
        });
        //第一个参数是接收参数类型, 第二个是返回参数类型
        CompletableFuture<String> thenApply1 = supplyFuture.thenApplyAsync((String acceptVal) -> {
            SleepUtils.sleep(2);
            System.out.println("thenApply1睡了两秒");
            System.out.println(Thread.currentThread().getName() + "第一个线程thenApply" + acceptVal);
            return acceptVal + "apply1";
        });

        CompletableFuture<String> thenApply2 = supplyFuture.thenApplyAsync((String acceptVal) -> {
            SleepUtils.sleep(2);
            System.out.println("thenApply2睡了两秒");
            System.out.println(Thread.currentThread().getName() + "第二个线程thenApply" + acceptVal);
            return acceptVal + "apply2";
        });
        System.out.println(thenApply1.get());
        System.out.println(thenApply2.get());

    }

}
